<?php
/**
 * The page class displays the html template
 * @author Daniel Nightingale
 */
class Page {
	
	/* @var $pg Page*/
	private static $pg;
	
	private $pageContent;
	private $cssFiles;
	private $jsFiles;
	private $internalCss;
	private $internalJs;
	private	$pageTitle;
	private	$bareContent;
	
	private function __construct() {
		$this->initialise();
	}
	
	/**
	 * Returns an instance of the Page class
	 * @return Page
	 */
	public static function getInstance() {
		if (!isset(self::$pg)) 
			self::$pg = new Page();
		return self::$pg;
	}
	
	/**
	 * Sets all initial values and files
	 */
	public function initialise() {
		
		$this->pageContent = "";
		$this->cssFiles = array();
		$this->jsFiles = array();
		$this->interalCss = "";
		$this->internalJs = "";
		$this->pageTitle = "";
		$this->bareContent = false;
		
		return;
	}
	
	/**
	 * Resets the initial value
	 */
	public function clear() {
		$this->initialise();
	}
	
	/**
	 * Sets the page content
	 * (template content)
	 * @param String $content
	 */
	public function setContent($pageContent) {
		$this->pageContent = $pageContent;
	}
	
	/**
	 * Adds a .css file
	 * @param String $cssFile
	 * @param String $mediaType
	 */
	public function addCss($cssFile, $mediaType = NULL) {
		$this->cssFiles[$cssFile] = $mediaType;
	}
	
	/**
	 * Adds an inline .css
	 * @param String $internalCss
	 */
	public function addInteralCss($internalCss) {
		$this->interalCss .= $internalCss;
	}
	
	/**
	 * Adds a .js file
	 * @param String $jsFile
	 */
	public function addJS($jsFile) {
		$this->jsFiles[] = $jsFile;
	}
	
	/**
	 * Adds inline .js
	 * @param String $internalJs
	 */
	public function addInternalJS($internalJs) {
		$this->internalJs .= $internalJs;
	}
	
	/**
	 * Set the title of the page
	 * @param String $pageTitle
	 */
	public function setTitle($pageTitle) {
		$this->pageTitle = $pageTitle;
	}
	
	/**
	 * Returns the page content
	 * @return string
	 */
	private function getContent() {
		return $this->pageContent;
	}
	
	/**
	 * Returns an array of all the Css files
	 * @return Array:
	 */
	private function getCss() {
		return $this->cssFiles;
	}
	
	/**
	 * Returns an array of all JS files
	 * @return Array:
	 */
	private function getJS() {
		return $this->jsFiles;
	}
	
	/**
	 * Returns an array of all internal styles
	 * @return Array
	 */
	private function getInternalCss() {
		return $this->internalCss;
	}
	
	/**
	 * Returns an array of all internal javascript
	 * @return Array
	 */
	private function getInternalJs() {
		return $this->internalJs;
	}
	
	/**
	 * Returns the title of the page
	 * @return string
	 */
	private function getTitle() {
		return $this->pageTitle;
	}
	
	private function getCssTags() {
		
		$this->addCss('includes/bootstrap-3.0.0/dist/css/bootstrap.css');
		$this->addCss('includes/bootstrap-3.0.0/dist/css/bootstrap-theme.css');
		
		$html = "";
		if ($this->getInternalCss() != '') {
			$html .= "<style type='text/css'>";
			$html .= $this->getInternalCss();
			$html .= "</style>";
		}
		
		foreach (array_reverse($this->getCss()) as $css => $media) {
			if (!isset($media))
				$media = 'all';
			$html .= "\n".'<link href="'.$css.'" media="'. $media.'" rel="stylesheet" type="text/css" />';
		}
		
		return $html;
	}
	
	private function getJavascriptTags() {
		
		$this->addJS('includes/bootstrap-3.0.0/dist/js/bootstrap.js');
		$this->addJS('includes/bootstrap-3.0.0/dist/js/bootstrap.js');
		$this->addJS('js/jquery-1.10.2.min.js');
		
		$html = "";
				
		foreach (array_reverse($this->getJS()) as $jsFile) {
			$html .= "\n".'<script src="'.$jsFile.'" language="javascript" type="text/javascript" ></script>';
		}
		
		if ($this->getInternalJs() != '') {
			$html .= '<script language="javascript" type="text/javascript">';
			$html .= $this->getInternalJs();
			$html .= "</script>";
		}
		
		return $html;
	}
	
	private function getHeaderPage() {
		
		$loggedIn = '';
		$panel = '';
		if (auth::isLoggedIn()) {
			$loggedIn .= '<p class="text-right"><small>You are logged in as ... '. auth::getMember()->getName() .' <a href="/logout">(logout)</a></small></p>';
			$panel .= Page::getMenuPanel();
		}
		else {
			$loggedIn .= '<p class="text-right"><small>You are currently not logged in</small></p>';
		}
		
		$html = <<<CONTENT
			<div class="row">
				$loggedIn
				<h1>P10509725</h1>
				<div class="btn-group btn-group-justified">
					$panel
				</div>
			</div>
CONTENT;
		
		return $html;
	}
	
	public static function getMenuPanel() {
		return <<<CONTENT
			<a class="btn btn-default" role="button" href="/home-page">home</a>
			<a class="btn btn-default" role="button" href="/category">category</a>
CONTENT;
	}
	
	private function getFooterPage() {
		$html = '';
		$html .= "\n".'<div class="footer">';
		$html .= "\n".'';
		$html .= "\n".'</div>';
		
		return $html;
	}
	
	public function printPage() {
		$html = '';
		$html .=  '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
		$html .= "\n".'<html xmlns="http://www.w3.org/1999/xhtml">';
		$html .= "\n".'<head>';
		$html .= "\n".'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		$html .= "\n".'<title>p10509725 - '.
		$title = "";
		if ($this->getTitle() != '') 
			$html .= $this->getTitle(); 
		$html .= '</title>';
		
		$baseHref = 'http://'.$_SERVER['HTTP_HOST'];
		$html .= "\n".'<base href="'.$baseHref.'" />';
		
		$html .= $this->getJavascriptTags();
		$html .= $this->getCssTags();
		
		
		$html .= "\n".'</head>';
		$html .= "\n".'<body>';
		$html .= "\n".'<div class="container">';
		$html .= $this->getHeaderPage();
		$html .= $this->getContent();
		$html .= $this->getFooterPage();
		$html .= "\n".'</div>';
		$html .= "\n".'</body>';
		$html .= "\n".'</html>';
		
		echo $html;
	}
}